<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * ajax上传
 */
namespace Addon\MallTemplateDefault\Controller;
use Addon\MallTemplateDefault\Controller\CommonController;

/**
 * AjaxUpload上传插件 TODO上传其他类型文件
 */
class MallController extends CommonController {
    
	//商品
	public function Index(){
		
		$m_mall_content = M('mall_content');
		
		$map['type'] = get_mall_model_conf('MALL_GOODS');
		$map['status'] = 1;
		
		
		$list = $m_mall_content->where($map)->order('time desc')->select();
	
		$this->assign('list',$list);
		
		$this->view();
	}
	
	
	//购物车
	public function Cart(){
		
	
		
		$this->view();
	}
	
	//创建订单
	public function Order(){
		
		dump(I());
		
		$m_mall_order = M('mall_order');
		
		//格式化订单
		$itemcount=I('param.itemCount','','');
		
		$m_mall_content = M('mall_content');
		
		if(!$itemcount){$this->error('你没有买东西！');die;}
		
		//循环获得购物车内信息
		for($i=1;$i<=$itemcount;$i++){
			
			//获得商品ID
			$order_info[$i]['goods_id'] = $this->getGoodsID(I('param.item_options_'.$i)); //商品ID
			
			$goods_info = $this->getGoodsInfo($order_info[$i]['goods_id']); //商品信息
			
			$order_info[$i]['goods_num']  = I('param.item_quantity_'.$i); //商品数量
			$order_info[$i]['name']  = $goods_info['name']; //商品名称
			$order_info[$i]['goods_money']  = $goods_info['money']; //商品单额
			$order_info[$i]['money']  = $order_info[$i]['goods_num']*$goods_info['money']; //商品总额
			$order_info[$i]['goods_partner_money']  = $order_info[$i]['goods_num']*$goods_info['partner_money']; //获得佣金
			$order_info[$i]['goods_integral']  = $order_info[$i]['goods_num']*$goods_info['member_integral']; //获得积分
			$order_info[$i]['goods_time']  = $goods_info['time']; //商品快照
			$order_info[$i]['goods_img']  = $goods_info['img']; //商品快照
			$order_info[$i]['status']  = 1; //默认都是1
			
		}
		
		$order['name'] = '商城订单'; //订单名称
		
		foreach($order_info as $oi=>$ok){
			
			$order['money'] = $order['money']+$order_info[$oi]['money'];
			$order['goods_money'] = $order['goods_money']+$order_info[$oi]['goods_money'];
			$order['goods_num'] = $order['goods_num']+$order_info[$oi]['goods_num'];
			$order['goods_partner_money'] = $order['goods_partner_money']+$order_info[$oi]['goods_partner_money'];
			$order['goods_integral'] = $order['goods_integral']+$order_info[$oi]['goods_integral'];
			
			
		}
		
		$order['order_type'] = get_mall_order_conf('MALL_ORDER_TYPE_GOODS'); //订单类型
		//$order['pay_type'] = 4; //订单类型
		//$order['time'] = time(); //订单类型
		$order['ip'] = get_client_ip(); //订单类型
		//$order['partner_id'] = 2; //订单类型
		//$order['express_type'] = 1; //订单类型
		//$order['status'] = get_mall_order_conf('MALL_ORDER_TYPE_1'); //订单类型
		$order['order_id'] = $this->getOrderCode('MALL_ORDER_TYPE_GOODS'); //订单类型
		$order['day'] = getToday(); //订单类型
		
		//测试数据
		$order['status'] = rand(1,9);
		$order['time'] = rand(1413633250,1463633250);
		$order['partner_id'] = rand(1,10);
		$order['pay_type'] = rand(1,4);
		$order['express_type'] = rand(1,3);
		
		$m_mall_order->add($order);
		
		foreach($order_info as $ooi=>$ook){
			
			$order_info[$ooi]['order_id'] = $order['order_id']; //订单类型
			$order_info[$ooi]['form_order_id'] = $order['order_id']; //订单类型
		
			$m_mall_order->add($order_info[$ooi]);
		};
	
		
		dump($order_info);
		dump($order);
		die;
	}
	
	//获得订单号
	public function getOrderCode($type){
		
		$m_mall_order = M('mall_order');
		
		$map['order_type'] = get_mall_order_conf($type);
		$map['form_order_id'] = 0;
		$map['day'] = getToday();
		
		$num = $m_mall_order->where($map)->count();
	
		$num = sprintf('%05s',$num+1);
		
		
		$code = date('Ymd',time()).$map['order_type'].$num;
		
		
		return $code;
		
	}
	
	//获得商品ID
	public function getGoodsID($info){
		
		$info = explode(",",$info);
			
		$goods_info = $info[0];
		
		$goods_info = explode(": ",$goods_info);
		
		
		$goods_id = $goods_info[1];
		
		return $goods_id;
		
	}
	
	//获得商品信息
	public function getGoodsInfo($id){
		
		$m_mall_content = M('mall_content');
		
		$map['id'] = $id;
		
		$data = $m_mall_content->where($map)->find();
		
		
		return $data;
		
	}
	
}